﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;
/// <summary>
/// 数组拓展
/// </summary>
public static class ArrayExtension
{
    /// <summary>
	/// 判断数组中是否存在某个值
	/// </summary>
	/// <param name="str"></param>
	/// <returns></returns>
	public static bool Contain<T>(this T[] tArray, params T[] t)
    {
        for (int i = 0; i < tArray.Length; i++)
        {
            for (int j = 0; j < t.Length; j++)
            {
                if (Equals(tArray[i], t[j]))
                {
                    return true;
                }
            }
            
            
        }
        return false;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sum"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <returns></returns>
    public static int[] GetRandomValues(int sum, int min, int max)
    {
        int[] arr = new int[sum];
        int j = 0;
        //表示键和值对的集合。
        Hashtable hashtable = new Hashtable();
        System.Random rm = new System.Random();
        while (hashtable.Count < sum)
        {
            //返回一个min到max之间的随机数
            int nValue = rm.Next(min, max);
            // 是否包含特定值
            if (!hashtable.ContainsValue(nValue))
            {
                //把键和值添加到hashtable
                hashtable.Add(nValue, nValue);
                arr[j] = nValue;

                j++;
            }
        }

        return arr;
    }

    public static bool ContainStr(this string[] tArray, params string[] t)
    {
        for (int i = 0; i < tArray.Length; i++)
        {
            if(tArray[i].Contains(t))
            {
                return true;
            }
            
        }
        return false;
    }
    /// <summary>
    /// 获取随机值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="tArray"></param>
    /// <returns></returns>
    public static T GetRandomValue<T>(this T[] tArray)
    {
        System.Random ran = new System.Random(RandomHelper.GetRandomSeed());
        return tArray[ran.Next(0, tArray.Length)];
    }
    public static T[] GetRandomValues<T>(this T[] tArray,int Count = 0)
    {
        System.Random ran = new System.Random(RandomHelper.GetRandomSeed());
        Hashtable hashtable = new Hashtable();
        List<T> temp = new List<T>();
        Count = Count == 0?Math.Max(1, ran.Next((int)(tArray.Length * 0.5f), (int)(tArray.Length * 0.7f))): Count;
        //Count =1;
        if (Count >= tArray.Length)
        {
            return tArray;
        }
        for (int i = 0; hashtable.Count < Count; i++)
        {
            int nValue = ran.Next(tArray.Length);
            if (!hashtable.ContainsValue(nValue))
            {
                hashtable.Add(nValue, nValue);
                temp.Add(tArray[nValue]);

            }
        }

        return temp.ToArray();

    }

    public static T[,] Split<T>(this T[,] tArray,int targetx,int targety)
    {
      
        T[,] te=new T[targetx, targety];
        int row= tArray.GetLength(0);
        int col= tArray.GetLength(1);
        if (targetx> row || col< targety)
        {
            return null;
        }
        int x = Random.Range(0, row - targetx);
        int y = Random.Range(0, col - targety);
        Debug.LogWarningFormat("起始位置是:({0},{1})", x, y);
        for (int i = x; i < x+ targetx; i++)
        {
            for (int j = y; j < y + targety; j++)
            {
                te[i - x, j - y] = tArray[i, j];
            }
        }
        return te;
    }

    /// <summary>
    /// 获取随机值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="tArray"></param>
    /// <returns></returns>
    public static T GetRandomValue<T>(this List<T> tArray,int startIndex=0)
    {
        System.Random ran = new System.Random(RandomHelper.GetRandomSeed());
        if(startIndex==tArray.Count)
        {
            return default;
        }
        return tArray[ran.Next(startIndex, tArray.Count)];
    }
    public static List<T> GetRandomValues<T>(this List<T> tArray, int Count)
    {
        System.Random ran = new System.Random(RandomHelper.GetRandomSeed());
        Hashtable hashtable = new Hashtable();
        List<T> temp = new List<T>();
        if (Count>= tArray.Count)
        {
            return tArray;
        }
       else
        {
            for (int i = 0; hashtable.Count < Count; i++)
            {
                int nValue = ran.Next(tArray.Count);
                if (!hashtable.ContainsValue(nValue))
                {
                    hashtable.Add(nValue, nValue);
                    temp.Add(tArray[nValue]);

                }
            }
          
            return temp;
        }
        
    }

    public static List<Vector3> FloatToVector(this List<float> vs)
    {
        if (vs == null)
        {
            return null;
        }
        List<Vector3> vectors = new List<Vector3>();
        for (int i = 0; i < vs.Count / 2; i++)
        {
            vectors.Add(new Vector3(vs[i * 2], vs[i * 2 + 1]));
        }
        return vectors;
    }

    public static void RemoveSome<T>(this List<T> tArray, List<T> remove)
    {
       
        for (int i = 0; i < remove.Count; i++)
        {
            if(tArray.Contains(remove[i]))
            {
                tArray.Remove(remove[i]);
            }
        }
        
    }

    public static void RemoveSome<T>(this List<T> tArray, T[] remove)
    {
        for (int i = 0; i < remove.Length; i++)
        {
            tArray.Remove(remove[i]);
        }

    }
    
    public static void TryAdd<T>(this List<T> tArray,T value)
    {
        //if (value == default) { return; }
        if(!tArray.Contains(value))
        {
            tArray.Add(value);
            
        }
    }

    /// <summary>
    /// 顺序添加
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="tArray"></param>
    /// <param name="value"></param>
    public static void AddOrdrRange<T>(this List<T> tArray, List<T> value)
    {
        for (int i = 0; i < value.Count; i++)
        {
            tArray.TryAdd(value[i]);
        }
    }

    

    /// <summary>
    /// 判断是否为null或数量为0
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="tArray"></param>
    /// <returns></returns>
    public static bool IsNullOrEmpty<T>(this List<T> tArray)
    {
        return (tArray == null || tArray.Count == 0);
    }

    public static bool IsNullOrEmpty<T>(this T[] tArray)
    {
        return (tArray == null || tArray.Length == 0);
    }

    public static void Print<T>(this T[,] array)
    {
        string str = null;
        for (int i = 0; i < array.GetLength(0); i++)
        {
           
            for (int j = 0; j < array.GetLength(1); j++)
            {
                str += array[i, j] + ",";
               
            }
            str += "\n";
           
        }
        Debug.LogWarning(str);

    }

    public static List<List<T>> ToList<T>(this T[,] array)
    {
        List<List<T>> list = new List<List<T>>();
        for (int i = 0; i < array.GetLength(0); i++)
        {
            List<T> temp = new List<T>(); 
            for (int j = 0; j < array.GetLength(1); j++)
            {
                temp.Add(array[i, j]);
            }
            list.Add(temp);
        }
     
        return list;
    }

    public static T[,] ToArray<T>(this List<List<T>> list)
    {
        T[,] temp = new T[list.Count, list.First().Count];
        for (int i = 0; i < list.Count; i++)
        {
            for (int j = 0; j < list[i].Count; j++)
            {
                temp[i, j] = list[i][j];
            }
        }
        return temp;

    }

    public static bool Contains<T>(this List<T> list, List<T> target)
    {
      
        for (int i = 0; i < target.Count; i++)
        {
            if(!list.Contains(target[i]))
            {
                return false;
            }
            
        }
        return true;
       
    }
}
